﻿//进入编辑状态
function edit() {
    AppGridE1.SetReadonly("LDAPServerGUID", false);
    AppGridE1.SetReadonly("DNPath", false);
    deDisplay("m_Edit,m_Sync,m_01");
    reDisplay("m_Save,m_Cancle");
}


//在编辑状态下取消编辑
function cancle() {
    deDisplay("m_Save,m_Cancle");
    reDisplay("m_Edit,m_Sync,m_01");
    AppGridE1.Refresh();
}

//在编辑状态下保存数据
function save() {
    ///校验
    if (!validate()) {
        AppGridE1.event.returnValue = false;
        return;
    }

    //保存数据
    strFile = "/Security/Security_XmlHTTP.aspx";
    sRtn = XmlHttpRequest(strFile, "SaveBULDAPMapping", "", AppGridE1.GetXml());
    if (sRtn) {
        var obj = eval('(' + sRtn + ')');
        if (obj.result) {
            //操作成功
            alert("保存成功！");
        }
        else {
            //操作失败
            alert(obj.message);
        }
    }
    deDisplay("m_Save,m_Cancle");
    reDisplay("m_Edit,m_Sync,m_01");
    AppGridE1.Refresh();
    //document.location.reload(true); 
}


function validate() {
    var rowCount, aryLDAPServer, aryDNPath;
    rowCount = AppGridE1.GetRowCount();
    aryLDAPServer = AppGridE1.GetCellsData("LDAPServerGUID");
    aryDNPath = AppGridE1.GetCellsData("DNPath");
    for (i = 0; i < rowCount; i++) {
        var buName = AppGridE1.GetAttribute("BUName", i);
        //校验同时配置域服务器和DN路径
        if ((aryLDAPServer[i] == "" && aryDNPath[i] != "")
            || (aryLDAPServer[i] != "" && aryDNPath[i] == "")) {
            alert(buName + "必须同时配置域服务器和DN路径！")
            AppGridE1.FocusCell(i, "DNPath");
            return false;
        }
        //校验域服务器是否可以连接
        if (aryLDAPServer[i] != "" && aryDNPath[i] != "") {
            strFile = "/Security/Security_XmlHTTP.aspx";
            sRtn = XmlHttpRequest(strFile, "CheckLDAP", "LDAPServerGUID=" + escape(aryLDAPServer[i]) + "&DNPath=" + escape(aryDNPath[i]));
            if (sRtn) {
                var obj = eval('(' + sRtn + ')');
                if (!obj.result) {
                    //操作失败
                    switch (obj.message) {
                        case "ServerError":
                            alert(buName + "验证域服务器时发生错误，请检查配置是否正确！");
                            return false;
                            break;

                        case "DNError":
                            alert(buName + "验证DN路径时发生错误，请检查配置是否正确！");
                            return false;
                            break;

                        default:
                            alert("验证域配置发生错误，请与管理员联系！");
                            return false;
                            break;
                    }
                }
            }
        }
    }

    return true;
}


//编辑域服务器
function editLDAPServer() {
    var sPath = "LDAPServer.aspx";
    var win = openMyWin(sPath);
    win.attachEvent("onunload", function() { document.location.reload(true) });
}

//同步用户过滤
function openUserSyncFilter() {
    var strPath = "UserSyncFilter.aspx";
    openMyWin(strPath);
}


//查看同步日志
function openSyncLog() {
    var strPath = "UserSyncLog.aspx";
    openMyWin(strPath);
}

//域用户同步
function syncUser() {
    var i = AppGridE1.GetSelectedRows();
    //未选择公司，提示“请选择进行域用户同步的公司”
    if (i < 0) { alert("请选择进行域用户同步的公司!"); return false; }

    var buName = AppGridE1.GetAttribute("BUName", i);
    //显示提示内容“确实要从域用户中同步XXX公司下的所有用户信息吗？”
    if (window.confirm("确实要从域用户中同步\"" + buName + "\"下的所有用户信息吗？") == false) { return false }

    var strFile = "/Security/Security_XmlHTTP.aspx";
    var buGUID = AppGridE1.GetAttribute("BUGUID", AppGridE1.GetSelectedRows())
    sRtn = XmlHttpRequest(strFile, "SyncUser", "BUGUID=" + escape(buGUID));
    if (sRtn) {
        var obj = eval('(' + sRtn + ')');
        if (obj.result) {
            //操作成功查看日志详情
            var sPath = "UserSyncLogDetail.aspx?mode=3&oid=" + escape(obj.message);
            openMyWin(sPath);
            try { window.parent.frmUser.appGrid.frameElement.Query(); } catch (e) { }

        } else {
            alert(obj.message);
        }
    } else {
        alert("同步失败！");
    }
}